package MyGO.controller;

import MyGO.Data.GlobalData;
import MyGO.Main;
import MyGO.controller.MyController.MyButton;
import MyGO.entity.Doctor;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;

import java.io.IOException;

import static MyGO.Data.GlobalData.stage;

public class Zong {
    public AnchorPane displayArea;
    private Doctor doctor;
    private String filtDoctorID;

    @FXML
    private Button back;
    @FXML
    private Rectangle z;
    @FXML
    private TitledPane guahao;

    @FXML
    private TitledPane jiancha;

    @FXML
    private TitledPane menzhen;
    @FXML
    private TitledPane yaofang;
    @FXML
    private Rectangle a1;
    @FXML
    private Rectangle a5;
    @FXML
    private Rectangle a6;
    @FXML
    private Rectangle a7;
    @FXML
    private Rectangle a8;
    @FXML
    private Rectangle a14;

    @FXML
    private Rectangle a13;

    @FXML
    private AnchorPane show;

    @FXML
    private VBox doctor_main_l;
    @FXML
    private Accordion lie;

    public void initialize() throws IOException {
        //设置返回登录界面
        back.setOnAction(event -> {
            FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("/MyGO/fxml/login.fxml"));
            Scene scene = null;
            try {
                scene = new Scene(fxmlLoader.load());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            stage.setScene(scene);
            stage.show();
        });

        //初始化数据
        doctor = GlobalData.doctor;
        lie.getPanes().clear();

        //登录后的根据权限显示
        switch(LoginControl.getStatus()){
            case 0:
                lie.getPanes().add(guahao);
                break;
            case 1:
                if (doctor == null) {
                    filtDoctorID = null;
                } else {
                    filtDoctorID = doctor.getId();
                }
                MyButton button = new MyButton("患者查看", "doctor1.png", "tableView.fxml", show,
                        "label_way", "患者查看", "未叫号", filtDoctorID, "1", "1");
                doctor_main_l.getChildren().add(button);


                MyButton button1 = new MyButton("检查申请", "doctor1.png", "check_doctor_view.fxml", show,
                        "label_way", "检查申请", null, doctor.getDepartment().toString(), "2", null);
                doctor_main_l.getChildren().add(button1);

                MyButton button2 = new MyButton("看诊记录", "doctor1.png", "tableView.fxml", show,
                        "label_way", "在诊患者", "已检查", filtDoctorID, "1", "2");
                doctor_main_l.getChildren().add(button2);

                MyButton button3 = new MyButton("开设处方", "doctor1.png", "prescri_Medication.fxml", show,
                        "label_way", "开设处方", null, null, "3", null);
                doctor_main_l.getChildren().add(button3);

                MyButton button4 = new MyButton("门诊确诊", "doctor1.png", "diagnose.fxml", show,
                        "label_way", "门诊确诊", null, null, "4", null);
                doctor_main_l.getChildren().add(button4);

                MyButton button5 = new MyButton("已确诊患者", "doctor1.png", "tableView.fxml", show,
                        "label_way", "已确诊患者", "完成", filtDoctorID, "1", "3");
                doctor_main_l.getChildren().add(button5);

                lie.getPanes().add(menzhen);
                break;
            case 2:
                lie.getPanes().add(jiancha);
                break;
            case 3:
                lie.getPanes().add(yaofang);
                break;
            default:
                lie.getPanes().addAll(guahao,menzhen,jiancha,yaofang);
        }

        a1.setOnMouseClicked(e -> {
            show.getChildren().clear();
            loadView(0);
        });
        a5.setOnMouseClicked(e -> {
            show.getChildren().clear();
            loadView(4);
        });
        a6.setOnMouseClicked(e -> {
            show.getChildren().clear();
            loadView(5);
        });
        a7.setOnMouseClicked(e -> {
            show.getChildren().clear();
            loadView(6);
        });
        a8.setOnMouseClicked(e -> {
            show.getChildren().clear();
            loadView(7);
        });
        a13.setOnMouseClicked(e -> {
            show.getChildren().clear();
            loadView(12);
        });
        a14.setOnMouseClicked(e -> {
            show.getChildren().clear();
            loadView(13);
        });
    }

    private void loadView(int viewIndex) {
    try {
        Node view;
        switch (viewIndex) {
            case 0:
                view = FXMLLoader.load(getClass().getResource("/MyGO/fxml/gua_hao.fxml"));
                break;
            case 4:
                view = FXMLLoader.load(getClass().getResource("/MyGO/fxml/shou_fei.fxml"));
                break;
            case 5:
                view = FXMLLoader.load(getClass().getResource("/MyGO/fxml/jiancha_jiancha_shenqing.fxml"));
                break;
            case 6:
                view = FXMLLoader.load(getClass().getResource("/MyGO/fxml/huanzhe_luru.fxml"));
                break;
            case 7:
                view = FXMLLoader.load(getClass().getResource("/MyGO/fxml/jieguo_luru.fxml"));
                break;
            case 12:
                view = FXMLLoader.load(getClass().getResource("/MyGO/fxml/fa_yao.fxml"));
                break;
            case 13:
                view = FXMLLoader.load(getClass().getResource("/MyGO/fxml/yaoku_guanli.fxml"));
                break;
            default:
                throw new IllegalArgumentException("Invalid view index");
        }
        Pane pane = new Pane(view);
        //调整位置
        pane.setLayoutY(5);

        show.getChildren().add(pane); // 添加新的视图
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
